// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters;

import java.util.ArrayList;
import zombie.ai.ZombieGroupManager;
import zombie.iso.IsoUtils;

public final class ZombieGroup {
    private final ArrayList<IsoZombie> members = new ArrayList();
    public float lastSpreadOutTime;

    public ZombieGroup reset() {
        this.members.clear();
        this.lastSpreadOutTime = -1.0F;
        return this;
    }

    public void add(IsoZombie zombie) {
        if (!this.members.contains(zombie)) {
            if (zombie.group != null) {
                zombie.group.remove(zombie);
            }

            this.members.add(zombie);
            zombie.group = this;
        }
    }

    public void remove(IsoZombie zombie) {
        this.members.remove(zombie);
        zombie.group = null;
    }

    public IsoZombie getLeader() {
        return this.members.isEmpty() ? null : (IsoZombie)this.members.get(0);
    }

    public boolean isEmpty() {
        return this.members.isEmpty();
    }

    public int size() {
        return this.members.size();
    }

    public void update() {
        int int0 = zombie.SandboxOptions.instance.zombieConfig.RallyTravelDistance.getValue();

        for (int int1 = 0; int1 < this.members.size(); int1++) {
            IsoZombie isoZombie = (IsoZombie)this.members.get(int1);
            float _float = 0.0F;
            if (int1 > 0) {
                _float = IsoUtils.DistanceToSquared(
                    ((IsoZombie)this.members.get(0)).getX(), ((IsoZombie)this.members.get(0)).getY(), isoZombie.getX(), isoZombie.getY()
                );
            }

            if (isoZombie.group != this || _float > (float)(int0 * int0) || !ZombieGroupManager.instance.shouldBeInGroup(isoZombie)) {
                if (isoZombie.group == this) {
                    isoZombie.group = null;
                }

                this.members.remove(int1--);
            }
        }
    }
}
